package com.qf.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.qf.model.entity.Doctor;
import com.qf.service.DoctorService;

import java.util.ArrayList;
import java.util.List;

/**
 * --- 天道酬勤 ---
 *
 * @author TRC
 * @date 2024/5/3
 * @desc
 */
public class DoctorListener extends AnalysisEventListener<Doctor> {

    private List<com.qf.model.entity.Doctor> doctorList = new ArrayList<>();

    private static final int BATCH_COUNT = 5;

    private DoctorService doctorService;

    public DoctorListener() {}

    public DoctorListener(DoctorService doctorService) {
        this.doctorService = doctorService;
    }

    @Override
    public void invoke(Doctor doctor, AnalysisContext analysisContext) {
        doctorList.add(doctor);
        if (doctorList.size() >= BATCH_COUNT) {
            doctorService.insertBatch(doctorList);
            doctorList.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (doctorList.size() > 0) {
            doctorService.insertBatch(doctorList);
            doctorList.clear();
        }
    }
}
